<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" >
      <el-form-item label="园区" prop="area">
        <el-select v-model="queryParams.area" placeholder="请选择园区"  style="width: 240px" @change="handleQuery">
          <el-option label="全部" value="" ></el-option>
          <el-option v-for="item in dict.type.user_area" :label="item.label" :value="item.value" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="菜品名称" prop="goodsId" v-show="showSearch">
        <el-select v-model="queryParams.goodsId" filterable placeholder="请选择菜品名称" @change="handleQuery" clearable @clear="handleQuery" >
          <el-option v-for="item in goodsList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item v-show="showSearch">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-form-item>
      <!--      <el-form-item >-->
      <!--        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>-->
      <!--      </el-form-item>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-form>
    <el-table v-loading="loading" :data="customerList" >
      <el-table-column label="序号"  prop="id"  fixed="left" align="center" width="80" />
      <el-table-column label="菜品名称"  prop="goodsName" align="center" width="120" />
      <el-table-column label="园区"  prop="area" align="center" width="100" />
      <el-table-column label="留样时间" align="center" prop="sampleTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sampleTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

<!--      <el-table-column label="留样保存至" align="center" prop="sampleToTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.sampleToTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="留样重量" align="center" prop="sampleNum" width="100"/>
      <el-table-column label="留样柜位置" align="center" prop="sampleLocation" width="120"/>
      <el-table-column label="保存条件" align="center" prop="saveConditions" width="120"/>
      <el-table-column label="留样人" align="center" prop="sampleBy" width="120"/>
      <el-table-column label="销毁日期" align="center" prop="destroyTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.destroyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="留样照片" prop="images" align="center" :width="imageColWidth">
        <template v-slot:default="scope">
          <image-preview v-for="(item,index) in scope.row.images"
                    style="width: 100px; height: 100px; margin-right: 15px"
                    :src="item">
          </image-preview>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center"  fixed="right"  width="160" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"

          >修改</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"

          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改食材配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="菜品名称"  prop="goodsId">
              <el-select v-model="form.goodsId" filterable placeholder="请选择"  >
                <el-option v-for="item in goodsList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
            <el-form-item label="留样柜位置" prop="sampleLocation">
<!--              <el-input  v-model="form.sampleLocation" placeholder="请输入留样柜位置" />-->
              <el-select v-model="form.sampleLocation" filterable placeholder="请选择留样柜位置"  >
                <el-option v-for="item in  dict.type.sample_location" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="留样时间" prop="sampleTime">
              <el-date-picker v-model="form.sampleTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd" @change="sampleTimeChange"
                              :style="{width: '100%'}" placeholder="请选择留样时间" :clearable="false"></el-date-picker>
            </el-form-item>

    <!--        <el-form-item label="留样保存至" prop="sampleToTime">-->
    <!--          <el-date-picker v-model="form.sampleToTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd"-->
    <!--                          :style="{width: '100%'}" placeholder="请选择留样保存至" clearable></el-date-picker>-->
    <!--        </el-form-item>-->

            <el-form-item label="留样重量(克)" prop="sampleNum">
<!--              <el-input  v-model="form.sampleNum" placeholder="请输入留样量" />-->
              <el-input-number v-model="form.sampleNum" controls-position="right" placeholder="请输入留样量"   :min="125" :precision="1"  ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="园区" prop="area">
              <el-select v-model="form.area" placeholder="请选择园区"  >
                <el-option  label="二区" value="二区"/>
                <el-option  label="一区" value="一区"/>
              </el-select>
            </el-form-item>

            <el-form-item label="保存条件" prop="saveConditions">
              <el-input  v-model="form.saveConditions" placeholder="请输入保存条件" />
            </el-form-item>

            <el-form-item label="销毁时间" prop="destroyTime" >
              <el-date-picker v-model="form.destroyTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd" :disabled="true"
                              :style="{width: '100%'}" placeholder="请选择销毁时间" clearable></el-date-picker>
            </el-form-item>
            <el-form-item label="留样人" prop="sampleBy">
              <el-input  v-model="form.sampleBy" placeholder="请输入留样人" />
<!--              <el-select v-model="form.sampleBy" filterable placeholder="请选择留样人"  >-->
<!--                <el-option v-for="item in  dict.type.sample_by" :key="item.value" :label="item.label" :value="item.value" />-->
<!--              </el-select>-->
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="留样照片" prop="images" >
          <ImageUpload
            v-model="form.images"
            :limit="4"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或减少客户余额对话框 -->

  </div>
</template>

<script>
  import { listAll, get, add, update ,list,del} from "@/api/examine/example/example";
  import { listAll as getGoodsList} from "@/api/shop/base/base";
  import dict from "@/utils/dict";
  import moment from "moment";
  import {changeRule} from "@/utils/formRules";
export default {
  name: "Example",
  dicts: ['sample_by','sample_location','user_area'],
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 食材表格数据
      customerList: [],
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        id: "",
        goodsName: "",
        pageNum: 1,
        pageSize: 10,
        area:'',
      },
      // 表单参数
      form: {
        sampleNum:125,
        saveConditions:'冷藏',
        destroyTime:'',
        sampleTime:'',
      },
      balanceForm: {
        type: 1,
        amount: 100,
        reason: undefined,
      },
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
        area:changeRule('请选择园区'),
        sampleTime:changeRule('请选择留样时间')
      },
      imageColWidth: 150,
    };
  },
  created() {
    this.queryParams.area=this.$store.state.user.userInfo.area || ''
    this.getList();
    getGoodsList().then(res => {

      this.goodsList = res.rows;
      // for (var i = 0; i < res.rows.length; i++) {
      //   this.goodsList.push({
      //     label: res.rows[i].name,
      //     value: res.rows[i].id
      //   });
      // }
    })
  },
  methods: {
    //修改留样时间
    sampleTimeChange(value){
      this.form.destroyTime=moment(value).add(2, 'days').format('YYYY-MM-DD')
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      list(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        let maxWith = 1;
        for (var i = 0; i < response.rows.length; i++) {
          response.rows[i].imageUrl = response.rows[i].images;
          response.rows[i].images = [];
          if (response.rows[i].imageUrl != null && response.rows[i].imageUrl != undefined) {
            response.rows[i].images = response.rows[i].imageUrl.split(",");
          }
          if (response.rows[i].images.length > maxWith) {
            maxWith = response.rows[i].images.length;
          }
        }
        this.imageColWidth = maxWith * 150;
          this.customerList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    getListAll() {
      this.loading = true;
      listAll(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.customerList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelBalance() {
      this.openBalance = false;
      this.resetBalance();
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    resetBalance() {
      this.resetForm("balanceForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form.id=''
      this.reset();
      this.open = true;
      this.title = "添加食材菜品留样";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      get(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改菜品留样";
      });
    },
    handleBalanceUpdate(row) {
      this.resetBalance();
      this.balanceForm.id = row.id;
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id) {
            update(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            add(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    submitBalanceForm: function() {
      updateInnerBalance(this.balanceForm).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.openBalance = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const roleIds = row.id || this.ids;
      this.$modal.confirm('是否确认序号为"' + roleIds + '"的数据项？').then(function() {
        return del(roleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
